package com.ylbh.modules.commentstar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ylbh.common.utils.Query;
import com.ylbh.modules.commentstar.entity.ShoppingCommentStar;
import com.ylbh.modules.commentstar.mapper.ShoppingCommentStarMapper;
import com.ylbh.modules.commentstar.service.ShoppingCommentStarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;


@Service("shoppingCommentStarService")
public class ShoppingCommentStarServiceImpl extends ServiceImpl<ShoppingCommentStarMapper, ShoppingCommentStar> implements ShoppingCommentStarService {

    @Autowired
    private ShoppingCommentStarMapper shoppingCommentStarMapper;

    /**
     * 获取评价模板列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryCommentStarList(Map<String, Object> params) {
        QueryWrapper<ShoppingCommentStar> wrapper = new QueryWrapper<>();
        if (null != params.get("keywords") && !"".equals(params.get("keywords").toString().trim())) {
            wrapper.eq("comment_content", params.get("keywords"));
        }
        if (null != params.get("starClass") && !"".equals(params.get("starClass").toString().trim())) {
            wrapper.eq("star_class", params.get("starClass"));
        }
        IPage<ShoppingCommentStar> page = this.page(
                new Query<ShoppingCommentStar>().getPage(params),
                wrapper
                .eq("state", 1)
                .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

    /**
     * 根据星级查询
     * @param starClass
     * @return
     */
    @Override
    public List<ShoppingCommentStar> queryStarList(Integer starClass) {
        return shoppingCommentStarMapper.selectStarList(starClass);
    }

    /**
     * 删除评价模板信息
     * @param asList
     */
    @Override
    public String removeCommentStarById(List<Integer> asList) {
        // 删除标签前要先查询该标签是否已经被运用
        for (Integer id : asList) {
            int count = shoppingCommentStarMapper.selectCommentClassByStarId(id);
            if (count > 0) {
                return "ID为：" + id +"被绑定";
            } else {
                shoppingCommentStarMapper.updateCommentStarById(id);
            }
        }
        return "成功";
    }
}
